package com.app.base.app;

import android.app.ProgressDialog;
import android.view.View;
import android.view.ViewGroup;
import http.okhttp.OkHttpUtils;
/**
 * 
 * 进入界面时，会有联网取数据的过程
 */
 abstract class BaseNetActivity extends BaseActivity {
	
	protected  Object TAG = new Object();
	protected ViewGroup rootView;
	protected View baseLoadView, baseErrorView, baseLoadedView,baseMesgView;
	
	protected ProgressDialog progDialog = null;
	
	protected abstract void showErrorView();
	protected abstract void showErrorView(String msg);
	protected abstract void showErrorView(int msg);
	

	
	/**
	 * 隐藏进度框
	 */
	protected void dissmissProgressDialog() {
		if (progDialog != null) {
			progDialog.dismiss();
		}
	}
	
	protected void showLoadingView() {
		if (baseLoadView!=null&&baseLoadView.getVisibility() == View.GONE) {
			baseLoadView.setVisibility(View.VISIBLE);
		}
		if(baseErrorView!=null&&baseErrorView.getVisibility() == View.VISIBLE){
			baseErrorView.setVisibility(View.GONE);
		}
		if(baseMesgView!=null&&baseMesgView.getVisibility() == View.VISIBLE){
			baseMesgView.setVisibility(View.GONE);
		}
		
		if(baseLoadedView!=null&&baseLoadedView.getVisibility() == View.VISIBLE){
			baseLoadedView.setVisibility(View.GONE);
		}
	}
	
	/**
	 * 显示进度框
	 */
	protected void showProgressDialog(String msg) {
		if (progDialog == null)
			progDialog = new ProgressDialog(this);
		progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progDialog.setIndeterminate(false);
		progDialog.setCancelable(false);
		progDialog.setMessage(msg);
		progDialog.show();
	}
	@Override
	public void setContentView(int layoutResID) {
		 super.setContentView(layoutResID);
		 baseLoadView =  initLoadView();
		 onLoad();
	}
	
	@Override
	public void setContentView(View view) {
		 super.setContentView(view);
		 baseLoadView =  initLoadView();
		 onLoad();
	}
	
	protected abstract ViewGroup initRootView();
	
	/**
	 * 初始化的时候执行<p> 
	 * <b>注意 : 只对于一进入就要取数据的Activity，以为返回的View会在所有View之上
	 * @return View 由Activity的根布局（RelativeLayout）添加到自己里,xml里要使用merge标签
	 */
	protected abstract View initLoadView();

	/**
	 * 第一次请求数据失败后显示的布局
	 * 
	 * @return
	 */
	protected abstract View initErrorView();

	/**
	 * 所有网络请求的入口函数
	 */
	protected abstract void onLoad();

	/**
	 * 对付空白页面，如果页面里没数据，可以调用
	 */
	protected  abstract void showMsgView( ViewGroup anchor,String msg);
	
	/**
	 * 等数据加载完后初始化目的布局
	 */
	protected abstract View initOnloadedView();
	
	protected ViewGroup getContentView() {
		return rootView;
	}

	protected View getBaseLoadView() {
		if(baseLoadView == null){
			baseLoadView = initLoadView();
		}
		return baseLoadView;
	}

	protected View getBaseErrorView() {
		if (baseErrorView == null) {
			baseErrorView = initErrorView();
		}
		return baseErrorView;
	}

	/**
	 * 
	 * 如果onloadedView没有初始化，先初始化，再返回
	 * 如果已经初始化就直接返回
	 * @param isDirect  true 直接返回，可能会返回null，false 如果为null 先初始化，保证不会为null
	 * @return
	 */
	protected View getBaseLoadedView(boolean isDirect) {
		if (!isDirect&&baseLoadedView == null) {
			baseLoadedView = initOnloadedView();
		}
		return baseLoadedView;
	}

	protected View getBaseLoadedView() {
		return getBaseLoadedView(false);
	}
	
	/**
	 * 取消tag标签的请求
	 */
	protected void cancelAll(Object tag){
		OkHttpUtils.getInstance().cancelTag(tag);
		//RequestHttp.getRequestQueue().cancelAll(tag);
	}
	protected void onDestroy() {
		super.onDestroy();
		cancelAll(TAG);
//		UsxApplication.getRequestQueue().cancelAll(new RequestQueue.RequestFilter() {
//	        @Override
//	        public boolean apply(Request<?> request) {
//	            // do I have to cancel this?
//	            return true; // -> always yes
//	        }
//	    });
	}
}
